<template>
    <el-button type="warning" icon="el-icon-plus" size="small" @click="int">导入</el-button>
    <el-dialog
        v-loading.fullscreen="isLoading"
        v-model="modal1"
        element-loading-text="导入中..."
        title="导入文件"
        class="customdialog"
        width="30%"
		:close-on-click-modal="false"
    >
        <p style="margin:5px 0 !important;color:#999;font-size:12px;">导入文件说明：</p>
        <p style="margin:5px 0 !important;color:#999;font-size:12px;">一、请不要有额外的字段</p>
        <p style="margin:5px 0 !important;color:#999;font-size:12px;">二、请严格按照示例文件格式导入</p>
		<el-row style="margin-top:20px;display: flex;align-items: center;">
			<UploadExcel :loadingstatus='isLoading' :onSuccess="importHander" class="mt10"></UploadExcel>
			<el-button
				type="text"
				class="mr15"
				style='margin-left: auto;'
				@click="downloadHander(template)"
			>下载示例文件</el-button>
		</el-row>
        <template #footer>
            <el-button @click="modal1 = false">关闭</el-button>
        </template>
		<el-dialog v-model="modal2" title="导入失败列表" width="600px" append-to-body :close-on-click-modal="false">
		  <el-table :data="error_list">
		    <el-table-column align="left" label="所在行" prop="number">
		      <template #scope="scope">
		        <span style="font-size: 12px">第{{scope.row.number}}行</span>
		      </template>
		    </el-table-column>
		    <el-table-column align="center" label="错误原因" prop="detail">
		      <template #scope="scope">
		        <span style="font-size: 12px">{{scope.row.detail}}</span>
		      </template>
		    </el-table-column>
		  </el-table>
		</el-dialog>
    </el-dialog>
</template>
<script>
import UploadExcel from "./upload";
import download from "downloadjs";
export default {
    name: "import",
    components: {
        UploadExcel
    },
    props: ['importurl', 'column', 'formatData', 'downurl', 'examname', 'type'],
    data() {
        return {
            modal1: false,
            modal2: false,
            isLoading: false,
            src: "",
            error_list: [],
            categoryId: "",
            categories: [],
            parentId: ''
        };
    },
    computed: {
        time() {
            return new Date().getTime();
        }
    },
    mounted() {
        
    },
    methods: {
        GMTToStr(time){
            let date = new Date(time)
            let year = date.getFullYear(); 
            let month = date.getMonth() + 1;
            let day = date.getDate() + 1;
            let Str = ''
            if(day < 10){
                Str = year + '-' + month + '-' + '0' + day
            }else{
                Str = year + '-' + month + '-' + day
            }
            return Str
        },
        int() {
            this.modal1 = true;
			this.isLoading = false
            this.src = "";
        },
        handleSuccess(res) {
            this.$message.success("上传成功");
            this.src = res.data.path;
        },
        exports() {},
        async importHander(data, gloadIndex = 0) {
			this.isLoading = true;
            // let _array = [];
			if (gloadIndex === 0) {
			    this.error_list = [];
			}
            data.results.forEach(async (ele, index) => {
				if (gloadIndex === index) {
					let _data = {};
					for (let c in this.column) {
					    if (ele[c]) {
					        _data[this.column[c]] = this.formatData ? this.formatFun(this.column[c], ele[c]) : ele[c];
					    }
					}
					if(this.type == 'people'){
                        _data.hint = true
						let res = await this.$API.system.personnel.action.post(_data).catch(err=>{
							this.isLoading = false;
						})
						if(res.error == 0){
							this.importHander(data, gloadIndex + 1);
						}else{
                            this.isLoading = false;
							this.error_list.push({
							    number: index + 2,
							    detail: res.message
							});
							this.importHander(data, gloadIndex + 1);
                        }
					}else if(this.type == 'industry'){ //行业导入
                        _data.import = 1
                        _data.hint = true
                        if(_data.lastname){
                            _data.parentId = this.parentId
                        }else {
                            _data.parentId = 0
                        }
						let res = await this.$API.system.industry.action.post(_data).catch(err=>{
							this.isLoading = false;
						})
						if(res.error == 0){
                            this.parentId = res.data.id
							this.importHander(data, gloadIndex + 1);
						}else{
                            this.isLoading = false;
                            this.parentId = res.data.id
							this.error_list.push({
							    number: index + 2,
							    detail: res.message
							});
							this.importHander(data, gloadIndex + 1);
                        }
					}
				}
            });
			
			if (gloadIndex > data.results.length - 1) {
			    this.isLoading = false;
			    this.$emit("onSuccess");
			    if (this.error_list.length > 0) {
			        this.modal1 = false;
			        this.modal2 = true;
			    } else {
			        this.modal1 = false;
			        this.$notify.success("导入成功");
			    }
			}
        },
        formatFun(data, value){
            let _return = ''
            this.formatData.map(item => {
                for(let c in item){
                    if(c === data){
                        for(let i in item[c]){
                            _return = item[c][value]
                            return i
                        }
                    }
                }
            })
            return _return
        },
        downloadHander() {
            let x = new XMLHttpRequest();
            x.open( "GET", this.downurl, true);
            x.responseType = "blob"
            x.onload = () => {
                download(x.response, this.examname + '.xls')
            };
            x.send();
        }
    }
};
</script>

<style scoped>
p {
  margin: 10px 0 !important;
}
</style>
